/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.rs.service.impl;

import java.sql.PreparedStatement;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import com.xy.biz.base.client.DictClient;
import com.xy.biz.fd.client.DocumentClient;
import com.xy.biz.fd.client.FolderClient;
import com.xy.biz.fd.domain.req.document.CopyDocumentRequest;
import com.xy.biz.fd.domain.req.folder.CreateManufactureFolderRequest;
import com.xy.biz.fd.domain.resp.GetDocumentResponse;
import com.xy.biz.fd.domain.resp.GetFolderResponse;
import com.xy.biz.rs.client.TechniqueClient;
import com.xy.biz.rs.domain.bo.ManufactureApplyBO;
import com.xy.biz.rs.domain.bo.ManufactureRecordTemplateBO;
import com.xy.biz.rs.domain.constants.ManufactureRecordConstants;
import com.xy.biz.rs.domain.query.ManufactureRecordTemplateQuery;
import com.xy.biz.rs.domain.req.GetTechniqueRequest;
import com.xy.biz.rs.domain.resp.GetTechniqueResponse;
import com.xy.biz.rs.manager.ManufactureRecordTemplateManager;
import com.xy.tool.context.UserContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xy.lang.domain.Result;
import com.xy.biz.rs.domain.bo.ManufactureRecordBO;
import com.xy.biz.rs.domain.query.ManufactureRecordQuery;
import com.xy.biz.rs.service.ManufactureRecordService;
import com.xy.biz.rs.manager.ManufactureRecordManager;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 *
 *
 * @author wwj
 * @date 2021-4-16
 */
@Service
public class ManufactureRecordServiceImpl implements ManufactureRecordService {

	@Autowired
	private ManufactureRecordManager manufactureRecordManager;
	@Autowired
	private DictClient dictClient;
	@Autowired
	private TechniqueClient techniqueClient;
	@Autowired
	private DocumentClient documentClient;
	@Autowired
	private FolderClient folderClient;
	@Autowired
	private ManufactureRecordTemplateManager manufactureRecordTemplateManager;

	@Transactional
	@Override
	public Result<?> saveManufactureRecord(ManufactureRecordBO manufactureRecordBO) {
		List<ManufactureRecordTemplateBO> manufactureRecordTemplateBOS = manufactureRecordTemplateManager.listManufactureRecordTemplate(ManufactureRecordTemplateQuery.one()
				.productDictCode(manufactureRecordBO.getProductDictCode())
				.subProductDictCode(manufactureRecordBO.getSubProductDictCode()).build());
		if (CollectionUtils.isEmpty(manufactureRecordTemplateBOS)) {
			return Result.ofBadRequest("获取记录模板失败");
		}
		ManufactureRecordTemplateBO manufactureRecordTemplateBO = manufactureRecordTemplateBOS.get(0);

		//复制记录文件
		Result<GetDocumentResponse> getDocumentResponseResult = documentClient.copyDocument(CopyDocumentRequest.builder()
				.documentId(manufactureRecordTemplateBO.getDocumentId())
				.destRepositoryId(manufactureRecordTemplateBO.getRepositoryId())
				.destFolderId(manufactureRecordTemplateBO.getFolderId())
				.documentName(manufactureRecordBO.getSubProductDictCode() + manufactureRecordBO.getBatchCode() + LocalDateTime.now().toString()).build());
		if (!getDocumentResponseResult.wasSuccess()) {
			return Result.ofBadRequest("记录文件复制失败");
		}
		GetDocumentResponse data = getDocumentResponseResult.getData();
		manufactureRecordBO.setDataRecordId(data.getDocumentId());


		builder(manufactureRecordBO);
		int affectedRows = manufactureRecordManager.saveManufactureRecord(manufactureRecordBO);
		if (affectedRows > 0) {
			return Result.success(data);
		}
		return Result.ofErrorRowsAffected();
	}

	private void builder(ManufactureRecordBO manufactureRecordBO){
		LocalDateTime now = LocalDateTime.now();
		manufactureRecordBO.setStatus(ManufactureRecordConstants.STATUS_RECORDING);
		manufactureRecordBO.setEditUserId(UserContextHolder.getUserId());
		manufactureRecordBO.setStartTime(now);
		manufactureRecordBO.setEndTime(now);
		manufactureRecordBO.setTenantId(UserContextHolder.getTenantId());
		manufactureRecordBO.setCreateUserId(UserContextHolder.getUserId());
		manufactureRecordBO.setCreateTime(now);
		manufactureRecordBO.setUpdateTime(now);
	}

	@Override
	public Result<?> saveAllManufactureRecord(Collection<ManufactureRecordBO> collection) {
		int affectedRows = manufactureRecordManager.saveAllManufactureRecord(collection);
		if (affectedRows > 0) {
			return Result.success();
		}
		return Result.ofErrorRowsAffected();
	}

	@Override
	public Result<?> updateManufactureRecord(ManufactureRecordBO manufactureRecordBO) {
		int affectedRows = manufactureRecordManager.updateManufactureRecord(manufactureRecordBO);
		if (affectedRows > 0) {
			return Result.success();
		}
		return Result.ofErrorRowsAffected();
	}

	@Override
	public Result<?> deleteManufactureRecord(Long manufactureRecordId) {
		int affectedRows = manufactureRecordManager.deleteManufactureRecord(manufactureRecordId);
		if (affectedRows > 0) {
			return Result.success();
		}
		return Result.ofErrorRowsAffected();
	}

	@Override
	public ManufactureRecordBO getManufactureRecord(Long manufactureRecordId) {
		return manufactureRecordManager.getManufactureRecord(manufactureRecordId);
	}

	@Override
	public List<ManufactureRecordBO> listManufactureRecordById(Collection<Long> collection) {
		return manufactureRecordManager.listManufactureRecordById(collection);
	}

	@Override
	public List<ManufactureRecordBO> listManufactureRecord(ManufactureRecordQuery query) {
		List<ManufactureRecordBO> manufactureRecordBOS = manufactureRecordManager.listManufactureRecord(query);
		//项目名设置值
		dictClient.buildByDict(manufactureRecordBOS,"product",bo->bo.getProductDictCode(),(bo,value)->bo.setProductDictValue(value));
		dictClient.buildByDict(manufactureRecordBOS,"product",bo->bo.getSubProductDictCode(),(bo,value)->bo.setSubProductDictValue(value));
		List<Long> techIds = manufactureRecordBOS.stream().map(ManufactureRecordBO::getTechniqueId).collect(Collectors.toList());
		//生产工艺名设置
		techniqueClient.buildByTechnique(manufactureRecordBOS,techIds,ManufactureRecordBO::getTechniqueId,(ma,t)->ma.setTechDocumentName(t.getDocumentName()));
		return manufactureRecordBOS;
	}

	@Override
	public int countManufactureRecord(ManufactureRecordQuery query) {
		return manufactureRecordManager.countManufactureRecord(query);
	}

}
